<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(A卷,100分)- 字符串重新排列、字符串重新排序（Java & JS & Python）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>给定一个字符串s&#xff0c;s包括以空格分隔的若干个单词&#xff0c;请对s进行如下处理后输出&#xff1a;<br /> 1、单词内部调整&#xff1a;对每个单词字母重新按字典序排序<br /> 2、单词间顺序调整&#xff1a;<br /> 1&#xff09;统计每个单词出现的次数&#xff0c;并按次数降序排列<br /> 2&#xff09;次数相同&#xff0c;按单词长度升序排列<br /> 3&#xff09;次数和单词长度均相同&#xff0c;按字典升序排列</p> 
<p>请输出处理后的字符串&#xff0c;每个单词以一个空格分隔。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>一行字符串&#xff0c;每个字符取值范围&#xff1a;[a-zA-z0-9]以及空格&#xff0c;字符串长度范围&#xff1a;[1&#xff0c;1000]</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>输出处理后的字符串&#xff0c;每个单词以一个空格分隔。</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">This is an apple</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">an is This aelpp</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">无</td></tr></tbody></table> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">My sister is in the house not in the yard</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">in in eht eht My is not adry ehosu eirsst</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">无</td></tr></tbody></table> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>本题需要注意的是&#xff0c;先进行单词内部调整&#xff0c;然后再进行单词间顺序。</p> 
<p>考察的是排序。</p> 
<p></p> 
<p>Python解法的第16行排序原理&#xff0c;请看&#xff1a;</p> 
<p><a href="https://fcqian.blog.csdn.net/article/details/129765379?spm&#61;1001.2014.3001.5502" rel="nofollow" title="Python3 - list sort方法实现多条件自定义排序_伏城之外的博客-CSDN博客">Python3 - list sort方法实现多条件自定义排序_伏城之外的博客-CSDN博客</a></p> 
<p></p> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">JavaScript算法源码</h4> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

rl.on(&#34;line&#34;, (line) &#61;&gt; {
  const arr &#61; line.split(&#34; &#34;);
  console.log(getResult(arr));
});

function getResult(arr) {
  arr &#61; arr.map((str) &#61;&gt; [...str].sort().join(&#34;&#34;));

  const count &#61; arr.reduce((p, c) &#61;&gt; {
    p[c] ? p[c]&#43;&#43; : (p[c] &#61; 1);
    return p;
  }, {});

  arr.sort((a, b) &#61;&gt;
    count[a] !&#61;&#61; count[b]
      ? count[b] - count[a]
      : a.length !&#61;&#61; b.length
      ? a.length - b.length
      : a &gt; b
      ? 1
      : -1
  );

  return arr.join(&#34; &#34;);
}
</code></pre> 
<p></p> 
<h4>Java算法源码</h4> 
<pre><code class="language-java">import java.util.Arrays;
import java.util.HashMap;
import java.util.Scanner;
import java.util.StringJoiner;

public class Main {
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);

    String[] arr &#61; sc.nextLine().split(&#34; &#34;);
    System.out.println(getResult(arr));
  }

  public static String getResult(String[] arr) {
    arr &#61;
        Arrays.stream(arr)
            .map(
                str -&gt; {
                  char[] cArr &#61; str.toCharArray();
                  Arrays.sort(cArr);
                  return new String(cArr);
                })
            .toArray(String[]::new);

    HashMap&lt;String, Integer&gt; count &#61; new HashMap&lt;&gt;();
    for (String s : arr) {
      count.put(s, count.getOrDefault(s, 0) &#43; 1);
    }

    Arrays.sort(
        arr,
        (a, b) -&gt;
            !count.get(a).equals(count.get(b))
                ? count.get(b) - count.get(a)
                : a.length() !&#61; b.length() ? a.length() - b.length() : a.compareTo(b));

    StringJoiner sj &#61; new StringJoiner(&#34; &#34;, &#34;&#34;, &#34;&#34;);
    for (String s : arr) {
      sj.add(s);
    }
    return sj.toString();
  }
}
</code></pre> 
<p></p> 
<h4>Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
arr &#61; input().split()


# 算法入口
def getResult(arr):
    for i in range(len(arr)):
        arr[i] &#61; &#34;&#34;.join(sorted(arr[i]))

    count &#61; {}
    for c in arr:
        if count.get(c) is None:
            count[c] &#61; 0
        count[c] &#43;&#61; 1

    # arr.sort(key&#61;lambda x: (-count[x], len(x), [ord(char) for char in x]))
    arr.sort(key&#61;lambda x: (-count[x], len(x), x))

    # return &#34; &#34;.join(map(str, arr))
    return &#34; &#34;.join(arr)


# 算法调用
print(getResult(arr))
</code></pre>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>